package com.dingwei.superiordiary.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.allen.library.SuperButton;
import com.allen.library.SuperTextView;
import com.dingwei.superiordiary.PayUtils.AliPayUtls;
import com.dingwei.superiordiary.PayUtils.PayInterface;
import com.dingwei.superiordiary.PayUtils.WeChatPayUtils;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.ConfirmOrderAdapter;
import com.dingwei.superiordiary.bean.ConfirmOrderBean;
import com.dingwei.superiordiary.bean.MyAddressBean;
import com.dingwei.superiordiary.bean.OrderPayBean;
import com.dingwei.superiordiary.mvp.presenter.ConfirmOrderPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.Constants;
import com.dingwei.superiordiary.utils.NumberUtil;
import com.dingwei.superiordiary.utils.PayResult;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.AppManager;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.MathUtil;
import com.lihao.baselibrary.weight.TitleBar;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/5/20.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */
public class ConfirmOrderActivity extends BaseActivity implements Contract.ConfirmOrderView {

    ConfirmOrderPresenter confirmOrderPresenter = new ConfirmOrderPresenter(this);

    ConfirmOrderAdapter adapter;
    String car_ids = "";
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.ll_address)
    LinearLayout llAddress;
    @Bind(R.id.mRecycleView)
    RecyclerView mRecycleView;
    @Bind(R.id.price)
    SuperTextView price;
    @Bind(R.id.et_remark)
    EditText etRemark;
    @Bind(R.id.btn_wx_pay)
    SuperButton btnWxPay;
    @Bind(R.id.btn_alipay)
    SuperButton btnAlipay;
    @Bind(R.id.data_layout)
    ScrollView dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private String address_id;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_confirm_order;
    }

    @Override
    protected void initData() {
        confirmOrderPresenter.getData(car_ids);
    }

    @Override
    protected void initView() {

        adapter = new ConfirmOrderAdapter(this);

        car_ids = getIntent().getStringExtra("car_ids");
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.setNestedScrollingEnabled(false);
        mRecycleView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.line), 1));

        mRecycleView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void setData(ConfirmOrderBean bean) {

        if (bean.data.default_address == null || TextUtils.isEmpty(bean.data.default_address.id)) {

            tvAddress.setText("添加地址");
            tvAddress.setTextColor(getResources().getColor(R.color.theme_color));
        } else {
            address_id = bean.data.default_address.id;
            tvAddress.setText(bean.data.default_address.name + " " + bean.data.default_address.mobile + " " + bean.data.default_address.province + bean.data.default_address.city + bean.data.default_address.county + bean.data.default_address.address);
            tvAddress.setTextColor(getResources().getColor(R.color.text_color_black));
        }

        adapter.clear();
        adapter.addAll(bean.data.product);

        price.setRightTopString("￥" + NumberUtil.getAmountValue(bean.data.total_price));

        if (bean.data.total_freight == 0) {
            price.setRightBottomString("免运费");
        } else {
            price.setRightBottomString("运费" + MathUtil.trimShu(bean.data.total_freight + "") + "元");
        }
        showData("success");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.ll_address, R.id.btn_wx_pay, R.id.btn_alipay, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_address:
                intent = new Intent(BaseActivity.context, MyAddressActivity.class);
                intent.putExtra("type", "1");
                startActivityForResult(intent, 1);
                break;
            case R.id.btn_wx_pay:
                confirmOrderPresenter.orderByCart("wx",car_ids,address_id);
                break;
            case R.id.btn_alipay:
                confirmOrderPresenter.orderByCart("alipay",car_ids,address_id);
                break;
            case R.id.no_net_btn:
                confirmOrderPresenter.getData(car_ids);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            MyAddressBean.DataBean dataBean = (MyAddressBean.DataBean) data.getSerializableExtra("address_info");
            address_id = dataBean.id;
            tvAddress.setText(dataBean.name + " " + dataBean.mobile + " " + dataBean.province + dataBean.city + dataBean.county + dataBean.address);
            tvAddress.setTextColor(getResources().getColor(R.color.text_color_black));
        }
    }

    IWXAPI api;
    @Override
    public void wxPay(OrderPayBean bean) {
        WeChatPayUtils.getInstance().setWeChatPay(bean.data.wx, new PayInterface() {
            @Override
            public void paySuccess() {
                intent = new Intent(BaseActivity.context,MyOrderActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishActivity(EditOrderActivity.class);
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void payError() {
                intent = new Intent(BaseActivity.context,MyOrderActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishActivity(EditOrderActivity.class);
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void payCancel() {
                intent = new Intent(BaseActivity.context,MyOrderActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishActivity(EditOrderActivity.class);
                AppManager.getAppManager().finishActivity();
            }
        },context);
    }




    @Override
    public void alipay(final OrderPayBean bean) {
        AliPayUtls.getInstance().setAliPay(this, bean.data.zfb, new PayInterface() {
            @Override
            public void paySuccess() {
                intent = new Intent(BaseActivity.context, MyOrderActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishActivity(EditOrderActivity.class);
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void payError() {
                intent = new Intent(BaseActivity.context, MyOrderActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishActivity(EditOrderActivity.class);
                AppManager.getAppManager().finishActivity();
            }

            @Override
            public void payCancel() {
                intent = new Intent(BaseActivity.context, MyOrderActivity.class);
                startActivity(intent);
                AppManager.getAppManager().finishActivity(EditOrderActivity.class);
                AppManager.getAppManager().finishActivity();
            }
        });
    }

}
